from fastapi import APIRouter
from . import scheams,models
from application.utils import wechat_tools

app = APIRouter()

@app.post('/register',response_model=scheams.UserInfoRegResponse)
async def register(user_info:scheams.UserInfoRegRequest):
    """用户注册"""
    # 0，验证数据【手机号是否重复注册，验证码】

    # 1，基于code请求微信服务器获取用户的OpenID以及将来调用用户信息的session_key
    result = wechat_tools.get_wechat_info(user_info.code)
    print(result)
    # 2,保存数据到用户表
    user = await models.User.create(
        **dict(user_info),
        username=user_info.mobile,
        avatar=user_info.avatarUrl,
        nickname=user_info.nickName,
        sex=user_info.gender,
        openid=result['openid']
    )

    # 3,返回结构
    return {
        'id':user.id,
        'nickname':user.nickname,
        'avatar':user.avatar,
        'code':200,
        'err_msg':'用户注册成功',
        'status':'Success',
        'token':'adgfsdjgklighklug'
    }